/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package network;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import weibo4j.Timeline;
import weibo4j.Weibo;
import weibo4j.model.Paging;
import weibo4j.model.Status;
import weibo4j.model.WeiboException;

/**
 *
 * @author Administrator
 */
public class testActivity {

    public static Map<String, Double> active = new HashMap<String, Double>();
    public static Map<String, String> name = new HashMap<String, String>();

    public static void main(String[] args) throws WeiboException, FileNotFoundException, IOException, InterruptedException, ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        DataPreparation data = new DataPreparation();
        prepareWeibo();
        relationScore.relationScore(data);
        setMap(data);
        analyse(data);
        //printResult();
        getResult();
    }

    public static void getResult() throws IOException {
        String filename="F:\\Sina\\weibo4j-oauth2-beta\\src\\network\\data\\" + Config.username + "statusActivityData.txt";
        BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
        BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("F:\\Sina\\weibo4j-oauth2-beta\\src\\network\\data\\" + Config.username + "statusActivityDataResult.txt")));
        String line=reader.readLine();
        line=reader.readLine();
        while(line!=null){
            if(!line.isEmpty()){
                
            }
        }
        writer.close();
        reader.close();
    }

    public static void printResult() {
        Set keys = active.keySet();
        int count = keys.size();
        for (int i = 0; i < count; i++) {
            String key = (String) keys.toArray()[i];
            System.out.println(name.get(key) + " " + active.get(key));
        }
    }

    public static void setMap(DataPreparation data) throws IOException {
        name = data.data.username;
        name.remove("0");
        name.remove(Config.uid);
    }

    public static void analyse(DataPreparation data) throws WeiboException, IOException, InterruptedException {
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("F:\\Sina\\weibo4j-oauth2-beta\\src\\network\\data\\" + Config.username + "statusActivityData.txt")));
        Timeline tm = new Timeline();
        Set keys = name.keySet();
        int count = keys.size();
        String title = "用户名\t用户类型\t影响力\t微博\t原创\t转发互粉普通人\t转发互粉名人\t转发名人\t转发媒体机构\t转发单向普通\t转发营销\t转发非关注\t被转发";
        System.out.println(title);
        writer.write(title + "\r\n");
        writer.flush();
        for (int i = 0; i < count; i++) {
            String key = (String) keys.toArray()[i];
            List<Status> status = null;
            while (status == null) {
                status = tm.getUserTimeline(key, name.get(key), 200, new Paging(1, 200), 0, 0);
                if (status != null) {
                    break;
                }
                Thread.sleep(3000);
                System.out.println("HttpRequest Time out");
            }
            int createdBlog = 0;
            int repostFromCelebrity = 0;
            int repostFromNormal = 0;
            int repostFromMedia = 0;
            int repostFromBf = 0;
            int repostFromZoombie = 0;
            int repostFromOther = 0;
            int repostCount = 0;
            int repostFromBfAndCelebrity = 0;
            for (Status s : status) {
                repostCount += s.getRepostsCount();

                if (s.getRetweetedStatus() != null) {
                    Status st = s.getRetweetedStatus();
                    if (st.getUser() != null) {
                        String id = st.getUser().getId();
                        if (data.data.biFCount.containsKey(id)) {
                            if (data.data.fameIDandTag.containsKey(id)) {
                                repostFromBfAndCelebrity += 1;
                            } else {
                                repostFromBf += 1;
                            }
                        } else if (data.data.fameIDandTag.containsKey(id)) {
                            if (data.data.mFIDs.contains(id)) {
                                repostFromCelebrity += 1;
                            } else {
                                repostFromOther += 1;
                            }
                        } else if (data.data.mediaIDs.contains(id) || data.data.agencyIDs.contains(id)) {
                            repostFromMedia += 1;
                        } else if (data.data.zoombieIDs.contains(id)) {
                            repostFromZoombie += 1;
                        } else if (data.data.mFIDs.contains(id)) {
                            repostFromNormal += 1;
                        } else {
                            repostFromOther += 1;
                        }
                    }
                } else {
                    createdBlog += 1;
                }

            }
            int sSize = status.size();
            if (sSize == 0) {
                continue;
            }
            long latestTime = status.get(0).getCreatedAt().getTime();
            long oldestTime = status.get(sSize - 1).getCreatedAt().getTime();
            double microblogFrequency = (double) sSize / ((latestTime - oldestTime) / 1000 / 60 / 60 / 24);
            active.put(key, microblogFrequency);
            String tag = "单向";
            if (data.data.fameIDandTag.containsKey(key)) {
                tag = "名人";
                if (data.data.mBiFIDs.contains(key)) {
                    tag = "互粉名人";
                }
            } else if (data.data.mBiFIDs.contains(key)) {
                tag = "互粉";
            } else if (data.data.mediaIDs.contains(key)) {
                tag = "媒体";
            } else if (data.data.agencyIDs.contains(key)) {
                tag = "机构";
            } else if (data.data.zoombieIDs.contains(key)) {
                tag = "营销用户";
            }
            double time = ((latestTime - oldestTime) / 1000 / 60 / 60 / 24);
            String output = name.get(key) + "\t"
                    + tag + "\t"
                    + data.influence.get(key) + "\t"
                    + microblogFrequency + "\t"
                    + (double) createdBlog / time + "\t"
                    + (double) repostFromBf / time + "\t"
                    + (double) repostFromBfAndCelebrity / time + "\t"
                    + (double) repostFromCelebrity / time + "\t"
                    + (double) repostFromMedia / time + "\t"
                    + (double) repostFromNormal / time + "\t"
                    + (double) repostFromZoombie / time + "\t"
                    + (double) repostFromOther / time + "\t"
                    + (double) repostCount / time;
            System.out.println(output);
            writer.write(output + "\r\n");
            writer.flush();
        }
        writer.close();
    }

    public static void prepareWeibo() {
        String access = Auth.Auth4Code.getCode();
        Weibo weibo = new Weibo();
        weibo.setToken(access);
    }
}
